var coinType = getUrlParams("coinType") || "ETH";
var base_url = poolConfig[coinType].url;
$(function() {
  $.each(poolConfig, function(key, item) {
    if(key != coinType) {
      var $li = $("<li>");
      var countSpan = $("<span>");
      countSpan.attr({
        "class": "new-block-count badge alert-success",
        "id": ("urltype" + key)
      });
      $("<a>").attr({
        "href": "/index.html?coinType=" + key
      }).text(key).append(countSpan).appendTo($li);
      if(key != "ETP") {
        $li.insertBefore($(".english-link-panel"));
      }
    }
  });
  var zecli = $("<li>");
  var zeccountSpan = $("<span>");
  zeccountSpan.attr({
    "class": "new-block-count badge alert-success",
    "id": ("urltypeZEC")
  });
  $("<a>").attr({
    "href": "http://zec.poolgpu.com/page/"
  }).text("ZEC").append(zeccountSpan).appendTo(zecli);
  if(coinType != "SC"){
    zecli.insertBefore($(".navbars").children().eq(6));
  } else {
    zecli.insertBefore($(".english-link-panel"));
    $(".sc-hide").hide();
  }
  var btxli = $("<li>");
  var btxcountSpan = $("<span>");
  btxcountSpan.attr({
    "class": "new-block-count badge alert-success",
    "id": ("urltypeBTX")
  });
  $("<a>").attr({
    "href": "http://btx.poolgpu.com"
  }).text("BTX").append(btxcountSpan).appendTo(btxli);
  btxli.insertBefore($(".english-link-panel"));
  var dcrli = $("<li>");
  var dcrcountSpan = $("<span>");
  dcrcountSpan.attr({
    "class": "new-block-count badge alert-success",
    "id": ("urltypeDCR")
  });
  $("<a>").attr({
    "href": "http://dcr.poolgpu.com"
  }).text("DCR").append(dcrcountSpan).appendTo(dcrli);
  dcrli.insertBefore($(".english-link-panel"));
  var btwoxli = $("<li>");
  var btwoxcountSpan = $("<span>");
  btwoxcountSpan.attr({
    "class": "new-block-count badge alert-success",
    "id": ("urltypeB2X")
  });
  $("<a>").attr({
    "href": "http://b2x.poolgpu.com"
  }).text("B2X").append(btwoxcountSpan).appendTo(btwoxli);
  btwoxli.insertBefore($(".english-link-panel"));
  if (poolConfig[coinType].tips) {
    $(".tips-panel p").html(poolConfig[coinType].tips);
    $(".tips-panel").removeAttr("hidden")
  }
  $(".english-link").attr("href", "http://www.poolgpu.com/" + coinType.toLocaleLowerCase());
  $(".nav-link").click(function() {
    event.preventDefault();
    location.href = $(this).attr("href").split("?")[0] + "?coinType=" + coinType;
  });
  $(".current-ciontype").text(coinType);
  $(poolConfig[coinType].poolSeting).removeClass("none");
  $(".search-btn").click(function() {
    if(!$(".accout-address").val()) {
      alert("请填写矿工地址");
      return;
    }
    location.href = "./miner.html?coinType=" + coinType + "&minerId=" + $(".accout-address").val()
  });
  $.get("http://zec.poolgpu.com:8999/allblocksnum.shtml", function(data) {
    $(".current-block-count").attr({
      "id": "urltype" + coinType
    });
    $("#urltypeETH").html(data.ethnum);
    $("#urltypeETC").html(data.etcnum);
    $("#urltypeEXP").html(data.expnum);
    $("#urltypeZEC").html(data.zecnum);
    $("#urltypeSC").html(data.scnum);
    $("#urltypeBTX").html(data.btxnum);
    $("#urltypeDCR").html(data.dcrnum);
    $("#urltypeB2X").html(data.btwoxnum);
  });
  $(".coin-types a").click(function() {
    location.href = "http://web.poolgpu.com/index.html?coinType=" + $(this).attr("coinType")
  });
});

function getUrlParams(name) {
  var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  var r = window.location.search.substr(1).match(reg);
  if(r) {
    return decodeURI(r[2]);
  } else {
    return null;
  }
}

function stringSub(text, len) {
  return text.length > len ? text.substring(0, len) + "..." : text;
}

function dateFormat(time, fmt) {
  if(!time) {
    return "";
  }
  var d = new Date(time);
  var o = {
    "M+": d.getMonth() + 1, //月份   
    "d+": d.getDate(), //日   
    "h+": d.getHours(), //小时   
    "m+": d.getMinutes(), //分   
    "s+": d.getSeconds(), //秒   
    "q+": Math.floor((d.getMonth() + 3) / 3), //季度   
    "S": d.getMilliseconds() //毫秒   
  };
  if(/(y+)/.test(fmt))
    fmt = fmt.replace(RegExp.$1, (d.getFullYear() + "").substr(4 - RegExp.$1.length));
  for(var k in o)
    if(new RegExp("(" + k + ")").test(fmt))
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  return fmt;
}

function unitCover(hashrate) {
  if(hashrate <= 0) {
    return hashrate
  }
  var i = 0;
  var units = ['H', 'KH', 'MH', 'GH', 'TH', 'PH'];
  while(hashrate > 1000) {
    hashrate = hashrate / 1000;
    i++;
  }
  return hashrate.toFixed(2) + ' ' + units[i];
}

function getBrowser() {
  var ua = navigator.userAgent.toLowerCase();
  var pc = "";
  if(ua.indexOf("ipad") !== -1) {
    pc = "pad";
  } else if(ua.indexOf("mobile") !== -1) {
    pc = "mobile";
  } else if(ua.indexOf("android") !== -1) {
    pc = "androidPad";
  } else {
    pc = "pc";
  }
  return {
    pc: pc,
    isMobile: (pc == "pc") ? false : true //是否是移动端  
  };
};

function hashTextCat(txt) {
  if(!getBrowser().isMobile) {
    return txt;
  }
  return txt.substring(0, 5) + "..." + txt.substring(txt.length - 5)
}
